package com.news.service.impl;

import com.github.pagehelper.PageHelper;
import com.news.mapper.CategoryMapper;
import com.news.mapper.NewsMapper;
import com.news.model.Category;
import com.news.model.CategoryExample;
import com.news.model.News;
import com.news.model.NewsExample;
import com.news.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<News> queryCategory(String id,int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        NewsExample news = new NewsExample();
        news.setDistinct(true);
        news.setOrderByClause("id");
        NewsExample.Criteria criteria = news.createCriteria();
        criteria.andCategoryIdEqualTo(id);
        List<News> news1 = newsMapper.selectByExample(news);
        return news1;
    }

    @Override
    public List<News> queryFiveNews(String id) {
        PageHelper.startPage(1,5);
        NewsExample newsExample = new NewsExample();
        NewsExample.Criteria criteria = newsExample.createCriteria();
        criteria.andCategoryIdEqualTo(id);
        criteria.andStatusEqualTo("0");
        newsExample.setOrderByClause("time desc");
        List<News> news = newsMapper.selectByExample(newsExample);
        return news;
    }

    @Override
    public List<News> orderData() {

        NewsExample newsExample = new NewsExample();
        newsExample.setOrderByClause("time");
        return newsMapper.selectByExample(newsExample);
    }

    @Override
    public List<News> hostPost() {
        NewsExample newsExample = new NewsExample();
        NewsExample.Criteria criteria = newsExample.createCriteria();
        criteria.andCategoryIdEqualTo("1");
        return newsMapper.selectByExample(newsExample);
    }

    @Override
    public News queryOne(String id) {
        return newsMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<News> queryAll(String id) {
        NewsExample newsExample = new NewsExample();
        NewsExample.Criteria criteria = newsExample.createCriteria();
        criteria.andCategoryIdEqualTo(id);
        return newsMapper.selectByExample(newsExample);
    }

    @Override
    public List<News> queryLike(String str) {
        NewsExample newsExample = new NewsExample();
        NewsExample.Criteria criteria = newsExample.createCriteria();
        newsExample.or().andTitleLike("%"+str+"%");
        return newsMapper.selectByExample(newsExample);
    }

}
